﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProyectoSalvadorDelMundo.CatalogosGrupales.Servidores
{
    public partial class ServiciosServidor : Helpers.PaginaBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                // Navegacion
                linkNav.NavigateUrl = ConfigurationManager.AppSettings.GetValues("linkNav-NavigateUrl")[0];
                linkNav.Text = ConfigurationManager.AppSettings.GetValues("linkNav-Text")[0];

                lblNavActiva.Text = ConfigurationManager.AppSettings.GetValues("lblNavActiva-Text")[0];
                lblNavActiva.CssClass = ConfigurationManager.AppSettings.GetValues("lblNavActiva-CssClass")[0];

                // Titulo
                lblTitulo.Text = ConfigurationManager.AppSettings.GetValues("lblTitulo-Text")[0];
                lblTitulo.CssClass = ConfigurationManager.AppSettings.GetValues("lblTitulo-CssClass")[0];

                //// Agregar
                //btnAgregar.Text = ConfigurationManager.AppSettings.GetValues("btnAgregar-Text")[0];
                //btnAgregar.ToolTip = ConfigurationManager.AppSettings.GetValues("btnAgregar-ToolTip")[0];
                //btnAgregar.CssClass = ConfigurationManager.AppSettings.GetValues("btnAgregar-CssClass")[0];

                //// Buscar 
                //btnBuscar.ImageUrl = ConfigurationManager.AppSettings.GetValues("btnBuscar-ImageUrl")[0];
                //btnBuscar.ToolTip = ConfigurationManager.AppSettings.GetValues("btnBuscar-ToolTip")[0];
                //btnBuscar.AlternateText = ConfigurationManager.AppSettings.GetValues("btnBuscar-AlternateText")[0];
                //btnBuscar.CssClass = ConfigurationManager.AppSettings.GetValues("btnBuscar-CssClass")[0];

                //txtBuscar.Attributes.Add("placeholder", ConfigurationManager.AppSettings.GetValues("txtBuscar-placeholder")[0]);
                //txtBuscar.CssClass = ConfigurationManager.AppSettings.GetValues("txtBuscar-CssClass")[0]; ;



                //this.Session["Grupo"] = "18";
                //this.Session["Asamblea"] = "18";
                //this.Session["Servidor"] = "18";
                //this.Session["NombreServidor"] 
                // Cargar datos.

                lblTitulo.Text = "Servicios disponibles para el servidor: " + this.Session["NombreServidor"];
                //this.Page.Form.DefaultButton = btnBuscar.ID;
                //this.Page.Form.DefaultFocus = txtBuscar.ID;
                RefrescarDatos();
            }
        }




        private DataTable Listar()
        {
            var oCn_Servicios = new CN.Servicios();
            //if (txtBuscar.Text != string.Empty)
            //{
            //    long asamblea = Convert.ToInt64(this.Session["Asamblea"]);
            //    long grupo = Convert.ToInt64(this.Session["Grupo"]);
            //    long servidor = Convert.ToInt64(this.Session["Servidor"]);
            //    return oCn_Servicios.ListarPorBus(txtBuscar.Text, Convert.ToInt64(this.Session["Grupo"]), Convert.ToInt64(this.Session["Asamblea"]));
            //}
            //else
            //{
            //    return oCn_Servicios.Listar(Convert.ToInt64(this.Session["Grupo"]), Convert.ToInt64(this.Session["Asamblea"]));
            //}

            return CN.Helpers.EjecutarConsultaText("SELECT	servicios.id_servicio, " +
		                                            "       servicios.nombre,servidores.id_servidor, " +
		                                            "       MAX(servidores.nombre),  " +
                                                    "       SUM(NOT isnull(servidores.id_servidor)) AS Aplica  " +
	                                                "   FROM servicios  " +
		                                            "       LEFT JOIN servicios_servidor ON servicios_servidor.id_servicio =  servicios.id_servicio " +
                                                    "       LEFT JOIN servidores  ON servidores.id_servidor = servicios_servidor.id_servidor AND servidores.id_servidor = " + this.Session["Servidor"] + " " +
                                                    "   WHERE   servicios.id_grupo = " + this.Session["Grupo"] + "  " +
                                                    "       AND servicios.id_asamblea = " + this.Session["Asamblea"] + "  " +
	                                                "   GROUP BY servicios.id_servicio, servicios.nombre  " +
	                                                "   ORDER BY servicios.nombre; " );
        }

        private void RefrescarDatos()
        {
            lsvLista.DataSource = Listar();
            lsvLista.DataBind();

        }

        protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            (lsvLista.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            RefrescarDatos();
        }


        protected void Guardar(object sender, EventArgs e)
        {
            var oCn_Servicios = new CN.Servicios();
            var oCe_Servicios = new CE.Servicios();
            var strResultado = string.Empty;
            var strId = "DELETE FROM servicios_servidor WHERE id_servidor = " + (string)this.Session["Servidor"] + "; INSERT INTO servicios_servidor (id_servidor, id_servicio) VALUES ";
            foreach (var item in lsvLista.Items.Where(i => ((CheckBox)i.FindControl("chkAplica")).Checked))
            {
                var label = (Label)item.FindControl("ID");

                strId += "(" + (string)this.Session["Servidor"] + ", " + label.Text + "),";
            }
            //if (lsvLista.Items.Count > 0)
            //{
            //    for (int i = 0; i < lsvLista.Items.Count; i++)
            //    {
            //        CheckBox PackageCheckBox = (CheckBox)lsvLista.Items[i].FindControl("Aplica");
            //        if (PackageCheckBox != null)
            //        {
            //            if (PackageCheckBox.Checked.Equals(true))
            //            {
            //                var label = (Label)lsvLista.Items[i].FindControl("id_servicio");

            //                strId = "(" + (string)this.Session["Servidor"] + ", " + label.Text + "),";
            //            }
            //        }
            //    }
            //}

            strId = strId.Trim().Trim(',');
            strId += ";";

            //oCe_Servicios.NOMBRE = txtNombre.Text;
            ////oCe_Servicios.ID_SERVICIO = lblAccion.Text == "Agregar" ? -1 : Convert.ToInt64(txtId.Text);
            //oCe_Servicios.ID_GRUPO_SERVICIO = Convert.ToInt64(ddlGrupoServicio.SelectedValue);
            //oCe_Servicios.ID_GRUPO = Convert.ToInt64(this.Session["Grupo"]);
            //oCe_Servicios.ID_ASAMBLEA = Convert.ToInt64(ddl.SelectedValue);
            //oCe_Servicios.ID_SERVICIO_RELACIONADO = Convert.ToInt64(ddlServicioRelacionado.SelectedValue); ;


            strResultado = CN.Helpers.EjecutarComandoText(strId);
            switch (strResultado)
            {
                case "Ocurrio un error inesperado al guardar los cambios.":
                    MostarError(strResultado);
                    break;
                default:
                    MostarExito(strResultado);
                    break;
            }
            Response.Redirect("~/CatalogosGrupales/Servidores/Servidores.aspx");
        }
        protected void Regresar(object sender, EventArgs e)
        {
            Response.Redirect("~/CatalogosGrupales/Servidores/Servidores.aspx");
        }
        protected void Buscar(object sender, EventArgs e)
        {
            RefrescarDatos();

        }
    }
}